package com.vdg.fps.data;

import java.util.Random;

import android.content.Context;

import com.vdg.fps.ulti.Utilities;

public class SettingData {
	private int mPostingThreads;
	private long mPostingDelayFrom;
	private long mPostingDelayTo;
	private boolean mAutoAddLinkThumbnailInYourMessage;
	private boolean mInclubNameWhenExportFacebook;
	private boolean mRemoveLink;
	private String mUserAgent;
	private static final String SETTING_KEY = "setting_key";
	private static SettingData sInstance;

	public static SettingData getInstance(Context context) {
		if (sInstance == null) {
			sInstance = new SettingData(
					Utilities.getStringFromSharedPreferences(SETTING_KEY,
							context));
		}
		return sInstance;
	}

	public int getPostingThreads() {
		return mPostingThreads;
	}

	public void setPostingThreads(int mPostingThreads, Context context) {
		this.mPostingThreads = mPostingThreads;
		save(context);
	}

	public long getPostingDelayFrom() {
		return mPostingDelayFrom;
	}

	public void setPostingDelayFrom(long mPostingDelayFrom, Context context) {
		this.mPostingDelayFrom = mPostingDelayFrom;
		save(context);
	}

	public long getPostingDelayTo() {
		return mPostingDelayTo;
	}

	public void setPostingDelayTo(long mPostingDelayTo, Context context) {
		this.mPostingDelayTo = mPostingDelayTo;
		save(context);
	}

	public boolean isAutoAddLinkThumbnailInYourMessage() {
		return mAutoAddLinkThumbnailInYourMessage;
	}

	public void setAutoAddLinkThumbnailInYourMessage(
			boolean mAutoAddLinkThumbnailInYourMessage, Context context) {
		this.mAutoAddLinkThumbnailInYourMessage = mAutoAddLinkThumbnailInYourMessage;
		save(context);
	}

	public boolean isInclubNameWhenExportFacebook() {
		return mInclubNameWhenExportFacebook;
	}

	public void setInclubNameWhenExportFacebook(
			boolean mInclubNameWhenExportFacebook, Context context) {
		this.mInclubNameWhenExportFacebook = mInclubNameWhenExportFacebook;
		save(context);
	}

	public String getUserAgent() {
		return mUserAgent;
	}

	public void setUserAgent(String mUserAgent, Context context) {
		this.mUserAgent = mUserAgent;
		save(context);
	}

	public String toString() {
		return String.valueOf(mPostingThreads) + "\n"
				+ String.valueOf(mPostingDelayFrom) + "\n"
				+ String.valueOf(mPostingDelayTo) + "\n"
				+ String.valueOf(mAutoAddLinkThumbnailInYourMessage) + "\n"
				+ String.valueOf(mInclubNameWhenExportFacebook) + "\n"
				+ String.valueOf(mRemoveLink) + "\n"
				+ String.valueOf(mUserAgent);
	}

	public SettingData(String data) {
		if (data != null && !data.equals("")) {
			String[] arrData = data.split("\n");
			if (arrData.length == 7) {
				mPostingThreads = Integer.valueOf(arrData[0]);
				mPostingDelayFrom = Long.valueOf(arrData[1]);
				mPostingDelayTo = Long.valueOf(arrData[2]);
				if (arrData[3] != null && !arrData[3].equals("")) {
					if (arrData[3].equals("true")) {
						mAutoAddLinkThumbnailInYourMessage = true;
					} else {
						mAutoAddLinkThumbnailInYourMessage = false;
					}
				}
				if (arrData[4] != null && !arrData[4].equals("")) {
					if (arrData[4].equals("true")) {
						mInclubNameWhenExportFacebook = true;
					} else {
						mInclubNameWhenExportFacebook = false;
					}
				}
				if (arrData[5] != null && !arrData[5].equals("")) {
					if (arrData[5].equals("true")) {
						mRemoveLink = true;
					} else {
						mRemoveLink = false;
					}
				}
				mUserAgent = arrData[6];
			}
		} else {
			mPostingThreads = 1;
			mPostingDelayFrom = 7000;
			mPostingDelayTo = 15000;
			mAutoAddLinkThumbnailInYourMessage = true;
			mRemoveLink = false;
			mInclubNameWhenExportFacebook = true;
			mUserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
		}
	}

	public void save(Context context) {
		Utilities.saveStringToSharedPreferences(SETTING_KEY, context,
				toString());
	}

	public boolean isRemoveLink() {
		return mRemoveLink;
	}

	public void setRemoveLink(boolean mRemoveLink) {
		this.mRemoveLink = mRemoveLink;
	}

	public int getRandomDelaySecondTime() {
		Random rn = new Random();
		int range = (int) ((mPostingDelayTo - mPostingDelayFrom) / 1000 + 1);
		int randomNum =  (rn.nextInt(range) + (int)(mPostingDelayFrom / 1000));
		return randomNum;
	}
}
